# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_with_harfbuzz="yes"
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]]; then
  _with_harfbuzz="no"
fi

_realname=freetype
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.11.0
pkgrel=2
pkgdesc="TrueType font rendering library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.freetype.org/"
license=(GPL2+ custom:FreeType)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
if [ "${_with_harfbuzz}" == "yes" ]; then
  depends+=("${MINGW_PACKAGE_PREFIX}-harfbuzz")
fi
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!libtool' 'staticlibs')
source=(https://download.savannah.gnu.org/releases/freetype/freetype-${pkgver}.tar.xz{,.sig}
        freetype-2.10.0-enable-valid.patch
        freetype-2.10.0-enable-spr.patch
        freetype-2.5.1-enable-sph.patch)
sha256sums=('8bee39bd3968c4804b70614a0a3ad597299ad0e824bc8aad5ce8aaf48067bde7'
            'SKIP'
            '30e84993f899b66c31451992667f7ac6cc9e0b19dfe2f47ccb546d77bbc77ef5'
            '0571f18d97834df020e4cbdb1338b14f9160669f868237080951e01f5b86727c'
            '324012ad68388d22c63368314721d2360b48dd40daa808d0383699a281d931b0')
validpgpkeys=('58E0C111E39F5408C5D3EC76C1A60EACE707FDA5')

prepare() {
  cd "${srcdir}/freetype-${pkgver}"

  plain "freetype-2.10.0-enable-valid.patch"
  patch -p1 -i "${srcdir}/freetype-2.10.0-enable-valid.patch"
  plain "freetype-2.10.0-enable-spr.patch"
  patch -p1 -i "${srcdir}/freetype-2.10.0-enable-spr.patch"
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  declare -a extra_config
  if [ "${_with_harfbuzz}" != "yes" ]; then
    extra_config+=("-Dharfbuzz=disabled")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  meson compile
}

package () {
  cd "${srcdir}/build-${CARCH}"

  DESTDIR="${pkgdir}" meson install

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/GPLv2.TXT"   "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GPLv2.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/FTL.TXT"     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/FTL.TXT"
}
